package com.ash.controller.message;

import com.ash.domain.Message;
import com.ash.domain.vo.MessageVo;
import com.ash.service.message.MessageService;
import com.ash.util.JsonUtil;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author : Chen Chao
 * @encoding : UTF-8
 * @date : 2021/4/3 19:52
 * @file : project06.com.ash.controller.message.MessageController.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
@Controller
public class MessageController {
    @Resource
    MessageService messageService;

    @RequestMapping("/user/message")
    public String message() {
        return "/user/message";
    }

    @PostMapping("/admin/message")
    @ResponseBody
    public Integer addMessage(Message message) {
        message.setCreateDate(new Date());
        return messageService.add(message);
    }

    @RequestMapping("/user/message/selectAll")
    @ResponseBody
    public String selectAll(@Nullable @RequestParam("rid") Integer rid, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        /*查询数据*/
        List<MessageVo> messageVos = messageService.selectByRole(rid, page, limit);
        int count = messageService.dataCount(rid);
        return JsonUtil.getJsonList(count, messageVos);
    }

    @RequestMapping("/admin/messageEdit")
    public String messageEdit() {
        return "admin/messageEdit";
    }

    @RequestMapping("/admin/message/delete")
    public Integer delete(@RequestParam("mid") Integer mid,HttpSession session) {
        return messageService.delete(mid);
    }

    @RequestMapping("/user/message/deleteByIds")
    @ResponseBody
    public Integer deleteByIds(@RequestParam("ids") List<Integer> ids) {
        return messageService.deleteByIds(ids);
    }

    @RequestMapping("/user/message/selectAllMessage")
    @ResponseBody
    public String selectAllMessage(@RequestParam("toId") Integer toId,
                                   @Nullable @RequestParam("isRead") Byte isRead,
                                   @Nullable @RequestParam("page") Integer page,
                                   @Nullable @RequestParam("limit") Integer limit, HttpSession session) {
        List<MessageVo> messageVos = messageService.selectByToId(toId, isRead, page, limit);
        session.setAttribute("messageCount",messageService.selectByToIdCount(toId, (byte) 0));
        return JsonUtil.getJsonList(messageService.selectByToIdCount(toId, isRead), messageVos);
    }

    @RequestMapping("/user/message/updateByIds")
    @ResponseBody
    public Integer updateByIds(@RequestParam("ids") List<Integer> ids, @RequestParam("toId") Integer toId) {
        return messageService.update(ids, toId);
    }

    @RequestMapping("/user/message/sendMessage")
    @ResponseBody
    public Integer sendMessage(Message message) {
        System.out.println(message);
        message.setHadRead((byte) 0);
        message.setCreateDate(new Date());
        System.out.println(message);
        return messageService.add(message);
    }
}
